package com.ayic.garbagerecycle.service.impl;

import com.ayic.garbagerecycle.mapper.StationMapper;
import com.ayic.garbagerecycle.mapper.WasteMapper;
import com.ayic.garbagerecycle.mapper.UserMapper;
import com.ayic.garbagerecycle.pojo.*;
import com.ayic.garbagerecycle.service.StationService;
import com.ayic.garbagerecycle.service.WasteService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class StationServiceImplement implements StationService {

    @Resource
    private StationMapper stationMapper;

    //获取所有站点
    @Override
    public List<Station> queryList(int offset, int pageSize) {
        return stationMapper.selectByPage(offset, pageSize);
    }

    @Override
    public int getTotalCount() {
        return stationMapper.count();
    }

    // 获取对应站点的全部数据
    @Override
    public Station getStationById(Integer stationId) {
        return stationMapper.selectById(stationId);
    }

    //更新站点的容量信息
    @Override
    public Result updateCurrentCapacity(Integer stationId) {
        stationMapper.updateCurrentCapacity(stationId);
        return Result.success("更新站点容量成功");
    }

    @Override
    public Result getStationAnalysis(String username) {
        Map <String,Station> StationMap = new HashMap<>();
//        List<Station> stations = stationMapper.selectByUsername(username);
        return Result.success();
    }


    @Override
    public List<String> getUniqueAreas() {
        List<String> locations = stationMapper.getAllStationLocations();
        Set<String> uniqueAreas = new HashSet<>();
        String regex = "^(.+?)[A-Z0-9]+"; // 匹配“X教”或类似格式
        for (String location : locations) {
            if (location != null) {
                java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(regex).matcher(location);
                if (matcher.find()) {
                    uniqueAreas.add(matcher.group(1));
                }
            }
        }
        return new ArrayList<>(uniqueAreas);
    }

    @Override
    public Map<String, List<StationInfoLite>> getStationsByAreas(List<String> areas) {

        List<StationInfoLite> stations = stationMapper.selectByLocations(areas);
        // 此时stations已经存放好了根据位置筛选的站点
        Map<String, List<StationInfoLite>> stationMap = new HashMap<>();

        // Group stations by area
        for (String area : areas) {
            List<StationInfoLite> stationsInArea = stations.stream()
                    .filter(StationInfoLite -> StationInfoLite.getLocation() != null && StationInfoLite.getLocation().contains(area))
                    .collect(Collectors.toList());
            stationMap.put(area, stationsInArea);
        }
        return stationMap;
    }

    @Override
    public List<AnalysisData> select30DayGroupedSumsByStationIds(List<Integer> ids) {
        return stationMapper.selectLast30DaysByStationId(ids);
    }

    @Override
    public List<AnalysisData> select30DayGroupedSumsByStationId(Integer stationId) {
        return stationMapper.selectLast30DaysBySingleStationId(stationId);
    }

    @Override
    public List<Station> getHighCapacityStationsByUser(String username) {
        // 获取用户负责的所有站点
        List<Station> stations = stationMapper.selectByUsername(username);
        
        // 使用Stream API过滤出容量使用率超过80%的站点
        return stations.stream()
                .filter(station -> {
                    // 检查currentCapacity和capacity是否为null
                    Integer currentCapacity = station.getCurrentCapacity();
                    Integer capacity = station.getCapacity();
                    if (currentCapacity == null || capacity == null || capacity == 0) {
                        return false;
                    }
                    // 计算容量使用率
                    double usageRate = (double) currentCapacity / capacity;
                    return usageRate >= 0.8; // 筛选使用率超过80%的站点
                })
                .collect(Collectors.toList());
    }
}
